package com.example.config.interceptorconfig;


import com.example.common.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    //    重写 addInterceptors 方法，将定义的拦截器添加进去，addPathPatterns 方法用来添加要拦截的请求，这里拦截所有的请求
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
//                .excludePathPatterns("/META-INF/resources/**")
//                .excludePathPatterns("/resources/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/public/**")
//                .excludePathPatterns("/file/**")
//                .excludePathPatterns("/commons/**")
                .excludePathPatterns("/h-jq/**")
                .excludePathPatterns("/h-ui/**")
                .excludePathPatterns("/h-ui.admin/**")
                .excludePathPatterns("/ui/**")
                .excludePathPatterns("/upload/**")
                .excludePathPatterns("/job/**")
                .excludePathPatterns("/jobManager.html")
                .excludePathPatterns("/resources/**")
                .excludePathPatterns("/favicon.ico")
                .excludePathPatterns("/error")
                //事件管理不拦截
                .excludePathPatterns("/pocEvent/**")

        ;
//        super.addInterceptors(registry);
    }

//    WebMvcConfigurationSupport 会导致默认的静态资源被拦截，需要手动将静态资源放开
// 除了在 MyInterceptorConfig 配置类中重写 addInterceptors 方法，还需要再重写一个方法 addResourceHandlers，用来将静态资源放开
//或者不继承 WebMvcConfigurationSupport 类，直接实现 WebMvcConfigurer 接口，然后重写 addInterceptors 方法，将自定义的拦截器添加进去即可
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
//        registry.addResourceHandler("/file/**").addResourceLocations("classpath:/file/");
//        registry.addResourceHandler("/ui/**").addResourceLocations("classpath:/static/ui/");
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//        super.addResourceHandlers(registry);
    }
}
